<template>
  <c-form :inline="true" :form="form"></c-form>
</template>

<script>
import Form from "@/components/Form";
import {userMultiInstance} from "@/components/bpmn/components/bpmnPropertiesPanel/forms";

export default {
  name: "UserTask_MultiInstance",
  components: {
    "c-form": Form,
  },
  data() {
    return {
      form: userMultiInstance.call(this),
    }
  },
  methods: {
    // 数据回填
    dataReset() {
      const modeler = this.modeler;
      const loopCharacteristics = modeler.getProperty("loopCharacteristics")
      if (loopCharacteristics) {
        const {loopCardinality, completionCondition, isSequential, collection, elementVariable} = loopCharacteristics;
        this.setMultiInstanceFieldsHide(false);
        this.setMultiInstanceFieldsData((isSequential ? "2" : "1"), loopCardinality?.body || "", completionCondition?.body || "", collection, elementVariable);
      } else {
        this.form.models.multiInstanceType = "0";
        this.setMultiInstanceFieldsHide(true);
      }
    },
    // 控制多实例输入框的显示
    setMultiInstanceFieldsHide(visible) {
      this.form.items.multiInstanceCardinality.hide = this.form.items.multiInstanceCollectionVarName.hide =
          this.form.items.multiInstanceElementVarName.hide = this.form.items.multiInstanceCompleteCondition.hide = visible;
    },
    // 更新多实例状态
    setMultiInstanceFieldsData(type, cardinality, condition, collectionVar, elementVar) {
      Object.assign(this.form.models, {
        multiInstanceType: type,
        multiInstanceCardinality: cardinality,
        multiInstanceCompleteCondition: condition,
        multiInstanceCollectionVarName: collectionVar,
        multiInstanceElementVarName: elementVar
      });
    },
    // 更改多实例类型
    changeMultiInstanceType(val) {
      const modeler = this.modeler;
      let loopCharacteristics = modeler.getProperty("loopCharacteristics");
      if (val !== "0") {
        const moddle = modeler.get("moddle");
        loopCharacteristics = moddle.create("bpmn:MultiInstanceLoopCharacteristics");
        loopCharacteristics.isSequential = val === "2";
      }
      this.setMultiInstanceFieldsHide(val === "0");
      modeler.updateProperties({
        loopCharacteristics: val === "0" ? undefined : loopCharacteristics
      });
      this.setMultiInstanceFieldsData(val, "", "", "", "");
    },
  }
}
</script>